package services;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.jws.WebService;

import model.User;

@WebService
public class Sudheer {

	/*public static void main(String args[])
	{
		Sudheer obj = new Sudheer();
		String s[] = obj.displayMovies("");
		System.out.println(s[0]+" "+s[1]+" "+s[2]+" "+s[3]+" "+s[4]+" "+s[5]+" "+s[6]+" "+
				s[7]+" "+s[8]+" "+s[9]+" "+s[10]+"  ");
	}*/

	public User[] displayUser(String s)
	{

		databaseConnection.Connection cn = new databaseConnection.Connection();
		Connection con = cn.getConnection();
		ArrayList<model.User> users = new ArrayList<model.User>(); 

		try {
			if(!con.isClosed())
			{
				/*this case is for when user enters nothing and searches for 
				 * which we have to display all the users*/
				if(s==null || s.length()==0)
				{


					PreparedStatement pstmt = con.prepareStatement("select * from videolibrary.VideoLibUsers where vlusr_rowstate !=-1 and vlusr_statusid = 1");
					//				pstmt.setString(1, constants.ConstantData.SCHEMA_NAME);
					ResultSet rt = pstmt.executeQuery();
					
					while(rt.next())
					{
						User user = new User();
						user.setId(rt.getInt(1));
						user.setSalutation(rt.getString(2));
						user.setFirstName(rt.getString(3));
						user.setMiddleName(rt.getString(4));
						user.setLastName(rt.getString(5));
						user.setDisplayName(rt.getString(6));
						user.setDob(String.valueOf(rt.getDate(7)));
						user.setSex(rt.getString(8));
						user.setLoginId(rt.getString(10));
						user.setLastPwdUpdate(String.valueOf(rt.getDate(12)));
						
						user.setEmailId(rt.getString(15));
						user.setLastLogin(String.valueOf(rt.getDate(17)));
						user.setSubscriptionRenewal(String.valueOf(rt.getDate(18)));
						users.add(user);
						
					} /* end of while*/
				} /* this one is for end of if*/
				/* this one is for if user enters particular userid*/
				else
				{
					System.out.println("else");
					PreparedStatement pstmt = con.prepareStatement("select * from videolibrary.VideoLibUsers where vlusr_id LIKE ?");
					pstmt.setString(1, s);
					ResultSet rt = pstmt.executeQuery();
					int i = 0;
					while(rt.next())
					{
						User user = new User();
						user.setId(rt.getInt(1));
						user.setSalutation(rt.getString(2));
						user.setFirstName(rt.getString(3));
						user.setMiddleName(rt.getString(4));
						user.setLastName(rt.getString(5));
						user.setDisplayName(rt.getString(6));
						user.setDob(String.valueOf(rt.getDate(7)));
						user.setSex(rt.getString(8));
						user.setLoginId(rt.getString(10));
						user.setLastPwdUpdate(String.valueOf(rt.getDate(12)));
						
						user.setEmailId(rt.getString(15));
						user.setLastLogin(String.valueOf(rt.getDate(17)));
						user.setSubscriptionRenewal(String.valueOf(rt.getDate(18)));
						users.add(user);


					}
				} /*end of else*/

			} /*end of outer if*/
			

			cn.getDisconnection();
		} 
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		User[] userArray = new User[users.size()];
		userArray = (User[]) users.toArray();
		return userArray;

		
	}


	public String[] displayMovies(String movie)
	{

		databaseConnection.Connection conn = new databaseConnection.Connection();
		Connection connection = conn.getConnection();
        ArrayList<String> arrayList = new ArrayList<String>();
		try {
			
			if(!connection.isClosed())
			{
				if(movie!=null && movie.length()!=0) /*this conditions is for checking that admin has entered a movie*/
				{
					System.out.println("movie entered");
					PreparedStatement pstmt =  connection.prepareStatement("select * from videolibrary.videolibmoviesmaster where vlmov_name LIKE ?");
					pstmt.setString(1, movie);
					ResultSet rt = pstmt.executeQuery();
					int i = 0;
					while(rt.next())
					{
						arrayList.add(i, Integer.toString(rt.getInt(1)));
						arrayList.add(i+1, rt.getString(2));
						arrayList.add(i+2, rt.getString(3));
						arrayList.add(i+3, Integer.toString(rt.getInt(4)));
						arrayList.add(i+4, String.valueOf(rt.getDate(5)));
						arrayList.add(i+5, String.valueOf(rt.getDouble(6)));
						arrayList.add(i+6, Integer.toString(rt.getInt(7)));
						arrayList.add(i+7, Integer.toString(rt.getInt(8)));
						arrayList.add(i+8, String.valueOf(rt.getDate(9)));
						arrayList.add(i+9, String.valueOf(rt.getDate(10)));
						arrayList.add(i+10, Integer.toString(rt.getInt(11)));
						i+=11;
					}
				} /*this one is closing if brace  */
				else
				{
					System.out.println("movie not entered");
					PreparedStatement pstmt =  connection.prepareStatement("select * from videolibrary.VideoLibMoviesMaster");
					ResultSet rt = pstmt.executeQuery();
					int  i = 0;
					while(rt.next())
					{
						arrayList.add(i, Integer.toString(rt.getInt(1)));
						arrayList.add(i+1, rt.getString(2));
						arrayList.add(i+2, rt.getString(3));
						arrayList.add(i+3, Integer.toString(rt.getInt(4)));
						arrayList.add(i+4, String.valueOf(rt.getDate(5)));
						arrayList.add(i+5, String.valueOf(rt.getDouble(6)));
						arrayList.add(i+6, Integer.toString(rt.getInt(7)));
						arrayList.add(i+7, Integer.toString(rt.getInt(8)));
						arrayList.add(i+8, String.valueOf(rt.getDate(9)));
						arrayList.add(i+9, String.valueOf(rt.getDate(10)));
						arrayList.add(i+10, Integer.toString(rt.getInt(11)));
						i+=11;
					}
				} /*this one is for closing else*/
				
				
				
				
				

			} /*end of if condition for connection*/
		} /* end of try*/ 
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String returnArray[] = new String[arrayList.size()];
		System.out.println("size="+arrayList.size());
		return arrayList.toArray(returnArray);
	} /*end of method for display movies*/

	public String[] latestMovie()
	{
		java.util.Date date = new java.util.Date();
		
		databaseConnection.Connection conn = new databaseConnection.Connection();
		Connection connection = conn.getConnection();
		ArrayList<String> arrayList = new ArrayList<String>();
		
		try {
			if(!connection.isClosed())
			{
				PreparedStatement pstmt = connection.prepareStatement("select * from videolibrary.videolibmoviesmaster where vlmov_releasedate");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		return null;
	}
}
